package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.THoverItemConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TLabelConfig;
	import com.NeuglsWorkStudio.Utility.Resource.ResourceLoader;
	import com.NeuglsWorkStudio.Utility.Resource.ResourceLoaderOption;
	import com.NeuglsWorkStudio.Utility.Utility;
	import com.NeuglsWorkStudio.Utility.nConsts;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * 实现 了一个有鼠标经过效果的Item,点击时激发一个事件。
	 * @author Neugls 06/29/2011
	 * */
	public class THoverItem extends TComponent
	{
		protected var _FBack:Sprite;
		protected var _FIcon:DisplayObject=null;
		protected var _FLabel:TLabel;
		private   var _FEnabled:Boolean;
		private var _FState:int=0;
		private var _FWidth:Number=120;
		
		public static const stNormal:int=0;
		public static const stMouseOver:int=1;
		public static const stMouseOut: int=2;
		public static const stMouseDown: int=3;
		public static const stMouseUp	:int=4;

		public function THoverItem(AConfig:TConfig)
		{
			super(AConfig);
		}
		

		public function get Enabled():Boolean
		{
			return _FEnabled;
		}

		public function set Enabled(value:Boolean):void
		{
			_FEnabled = value;
			this.buttonMode=_FEnabled;
		}

		public function get Width():Number
		{
			return _FWidth;
		}

		public function set Width(value:Number):void
		{
			_FWidth = value;
		}

		public function get State():int
		{
			return _FState;
		}

		public function set State(value:int):void
		{
			_FState = value;
		}
		
		public function get ExtraData():Object{
			return HoverItemConfig().ExtraData;
		}

		public function HoverItemConfig():THoverItemConfig{
			return _FConfig as THoverItemConfig;
		}
		
		override protected function Init():void{
			super.Init();
		
			_FBack=new Sprite();
			this.addChild(_FBack);
			
			var labelConfig:TLabelConfig=new TLabelConfig();
			labelConfig.AutoWordWrap=false;
			labelConfig.Caption=HoverItemConfig().Caption;
			labelConfig.Color=HoverItemConfig().NormalFontColor;
			labelConfig.Font=HoverItemConfig().NormalFontName;
			labelConfig.Name='hoveritemlabel';
			labelConfig.Size=HoverItemConfig().NormalFontSize;
			labelConfig.x=0;
			labelConfig.y=0;
			
			_FLabel=new TLabel(labelConfig);
			this.addChild(_FLabel);
			
			if(HoverItemConfig().DisplayIcon){
				if(HoverItemConfig().UseIconURL){
					ResourceLoader.LoadImage(HoverItemConfig().IconURL,
						IconLoaded,IconLoadError);
				}else{
					_FIcon=HoverItemConfig().Icon;
					Utility.ProcessDisplayObjectPosition(HoverItemConfig().IconW,
						HoverItemConfig().IconH,_FIcon,nConsts.bpFit);
					
				}
			}
			
			this.addEventListener(MouseEvent.CLICK,OnMouseClick,false,0,true);
			this.addEventListener(MouseEvent.MOUSE_OVER,OnMouseOver,false,0,true);
			this.addEventListener(MouseEvent.MOUSE_OUT,OnMouseOut,false,0,true);
			this.addEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown,false,0,true);
			this.addEventListener(MouseEvent.MOUSE_UP,OnMouseUp,false,0,true);
			
			this.buttonMode=true;
			this.mouseChildren=HoverItemConfig().EnableChildMouse;
			Width=HoverItemConfig().Width;
			Enabled=HoverItemConfig().Enabled;
			Paint();
		}
		
		private function IconLoaded(ALoader:Object,option:ResourceLoaderOption):void{
			_FIcon=new Bitmap(
				Utility.LoaderToBitmap(ALoader as Loader,HoverItemConfig().IconW,
				HoverItemConfig().IconH,true)
			);
			Paint();
		}
		private function IconLoadError(error:String,Option:ResourceLoaderOption):void{
			_FIcon=null;
			Paint();
		}
		
		private function Paint():void{
			switch(State){
				case stNormal:
				case stMouseOut:
					DrawNormal();
					break;
				case stMouseOver:
					DrawMouseOver();
					break;
				case stMouseDown:
					DrawMouseDown();
					break;
				case stMouseUp:
					DrawMouseUp();
			}
			
			DrawIconAndLabel();
		}
		
		protected function DrawIconAndLabel():void{
			var LastX:Number=5;
			
			if(_FIcon!=null){
				if(!this.contains(_FIcon))
					this.addChild(_FIcon);
				if(HoverItemConfig().IconDisplayHeight!=0&&HoverItemConfig().IconDisplayWidth!=0&&HoverItemConfig().CenterIcon){
					Utility.ProcessDisplayObjectPosition(HoverItemConfig().IconDisplayWidth,
						HoverItemConfig().IconDisplayHeight,
						_FIcon,nConsts.bpCenter);
					if(_FIcon.x<LastX)
						_FIcon.x=LastX;
					LastX+=HoverItemConfig().IconDisplayWidth+5;//5 is margin.
				}else{
					_FIcon.x=LastX;
					_FIcon.y=(HoverItemConfig().Height-_FIcon.height)/2;
					LastX+=_FIcon.width+5;//5 is margin.
				}
			}
			
			with(_FLabel){
				x=LastX;
				y=(HoverItemConfig().Height-height)/2;
			}
		}
		
		protected function DrawNormal():void{
			_FBack.graphics.clear();
		}
		
		protected function DrawMouseOver():void{
			if(HoverItemConfig().OnMouseOverDraw!=null){
				HoverItemConfig().OnMouseOverDraw(_FBack);
			}else{
				with(_FBack.graphics){
					clear();
					beginFill(HoverItemConfig().HoverBackColor,HoverItemConfig().HoverBackAlpha);
					drawRect(0,0,Width,HoverItemConfig().Height);
					endFill();
				}
			}
		}
		protected function DrawMouseDown():void{
			
		}
		
		protected function DrawMouseUp():void{
			
		}
		
		protected function OnClick():void{
			if(HoverItemConfig().OnClick!=null&&_FEnabled){
				HoverItemConfig().OnClick(HoverItemConfig().ExtraData);
			}	
		}
		
		private function OnMouseClick(E:MouseEvent):void{
			OnClick();
		}
		
		private function OnMouseOver(E:MouseEvent):void{
			State=stMouseOver;
			Paint();
		}
		
		private function OnMouseOut(E:MouseEvent):void{
			State=stMouseOut;
			Paint();
		}
		
		private function OnMouseDown(E:MouseEvent):void{
			State=stMouseDown;
			Paint();
		}
		
		private function OnMouseUp(E:MouseEvent):void{
			State=stMouseUp;
			Paint();
		}
		
		
		public function get Caption():String{
			return _FLabel.Caption;
		}
		public function set Caption(val:String):void{
			_FLabel.Caption=val;
			DrawIconAndLabel();
		}
	}
}